package leecode

import (
	"container/list"
	"sort"
)

func findTargetNode(root *TreeNode, cnt int) (result int) {
	//层序遍历
	arr := make([]int, 0)
	queue := list.New()
	if root == nil {
		return
	}
	queue.PushBack(root)
	for queue.Len() > 0 {
		size := queue.Len()
		for i := 0; i < size; i++ {
			node := queue.Remove(queue.Front()).(*TreeNode)
			if node.Left != nil {
				queue.PushBack(node.Left)
			}
			if node.Right != nil {
				queue.PushBack(node.Right)
			}
			arr = append(arr, node.Val)
		}
	}
	sort.Ints(arr)
	return arr[len(arr)-cnt]
}
